/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package seGobangClient.uiSystem2D;

import java.awt.Color;
import java.awt.Graphics;
import java.awt.Image;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.awt.event.MouseMotionListener;
import javax.swing.ImageIcon;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.vecmath.Point2i;
import seGobangResources.PathRes;

/**
 *
 * @author Leo
 */
class MyPanel extends JPanel implements MouseListener, MouseMotionListener
{

    private ImageIcon i;
    private ImageIcon b;
    private ImageIcon w;
   private RectPanel rp;
   private RectPanel lastput;
    private int x = -1;
    private int y = -1;
    private byte[][] pos = new byte[15][15];
    private PathRes path = new PathRes();
    boolean valid = false;
    public MyPanel()
    {
        super();
        try
        {
            i = new ImageIcon(path.getPath("Texture/Surface"));
            b = new ImageIcon(path.getPath("Texture/black"));
            
            w = new ImageIcon(path.getPath("Texture/white"));
        } catch (Exception e)
        {
            System.out.println(e);
        }
        i.setImage(i.getImage().getScaledInstance(390, 390, Image.SCALE_DEFAULT));
        b.setImage(b.getImage().getScaledInstance(26, 26, Image.SCALE_DEFAULT));
        w.setImage(w.getImage().getScaledInstance(26, 26, Image.SCALE_DEFAULT));
        this.setLayout(null);
        this.setSize(390, 390);
        this.addMouseListener(this);
        this.addMouseMotionListener(this);
        rp=new RectPanel(Color.red);
        this.add(rp);
        lastput=new RectPanel(Color.blue);
        lastput.setVisible(false);
        this.add(lastput);
    }

    public void mouseEntered(MouseEvent me)
    {
        
    }

    public void mouseExited(MouseEvent me)
    {
    }

    public void mousePressed(MouseEvent me)
    {

    }

    public void mouseReleased(MouseEvent me)
    {
    }

    public void mouseClicked(MouseEvent me)
    {
        if (valid)
        {
            int xx = me.getX();//-this.getX();
            int yy = me.getY();//-this.getY();
            for (int i = 0; i < 15; i++)
            {
                if ((double) xx >= (double) i * this.getWidth() / 15 && (double) xx < (double) (i + 1) * this.getWidth() / 15)
                {
                    x = i;
                }
            }
            for (int i = 0; i < 15; i++)
            {
                if ((double) yy >= (double) i * this.getHeight() / 15 && (double) yy < (double) (i + 1) * this.getHeight() / 15)
                {
                    y = i;
                }
            }
            x-=7;y-=7;
            System.out.println(x + "" + y);
            if((Boolean)SceneManager.MyCurrentController.doEvent("TestPosition", new Point2i(x,y)))
            {
                SceneManager.MyCurrentController.doEvent("ChooseChessBoard", new Point2i(x, y));
            }
        }
    }

    public void mouseMoved(MouseEvent me)
    {
int xx = me.getX();
            int yy = me.getY();
           
            for (int i = 0; i < 15; i++)
            {
                if ((double) xx >= (double) i * this.getWidth() / 15 && (double) xx < (double) (i + 1) * this.getWidth() / 15)
                {
                    x = i;
                }
            }
            for (int i = 0; i < 15; i++)
            {
                if ((double) yy >= (double) i * this.getHeight() / 15 && (double) yy < (double) (i + 1) * this.getHeight() / 15)
                {
                    y = i;
                }
            }
            if(pos[x][y]==0){
                rp.setVisible(true);
            rp.setLocation((int) ((double) x * this.getWidth() / 15),
                            (int) ((double) y * this.getWidth() / 15));
            }
            else
            {
                rp.setVisible(false);
            }
            repaint();
    }

    public void mouseDragged(MouseEvent me)
    {


    }

    public void PaddChess(int px, int py, byte borw)
    {
        
        x = px+7;
        y = py+7;
        pos[x][y]=borw;
        repaint();
        if(pos[x][y]==0)
            lastput.setVisible(false);
        else
        {lastput.setLocation((int) ((double) x * this.getWidth() / 15),
                            (int) ((double) y * this.getWidth() / 15));
        lastput.setVisible(true);}
        rp.setVisible(false);
    }


    public void PclearChessBoard()
    {
        for (int r = 0; r < 15; r++)
        {
            for (int c = 0; c < 15; c++)
            {
                pos[r][c] = 0;
            }
        }
        this.repaint();
    }
    //public void paint(Graphics g)
    //{
      //  g.drawRect(x, y, 26, 26);
  //  }
    protected void paintComponent(Graphics g)
    {
        super.paintComponent(g);

        java.awt.Color c = this.getBackground();
        
        g.drawImage(i.getImage(), 0, 0, c, null);
        for (int ro = 0; ro < 15; ro++)
        {
            for (int co = 0; co < 15; co++)
            {
                if (pos[ro][co]==(byte)-1)
                {
                    g.drawImage(b.getImage(), (int) ((double) ro * this.getWidth() / 15),
                            (int) ((double) co * this.getWidth() / 15), c, null);
                }
                if (pos[ro][co] == (byte)1)
                {
                    g.drawImage(w.getImage(), (int) ((double) ro * this.getWidth() / 15),
                            (int) ((double) co * this.getWidth() / 15), c, null);
                }

            }
        }

 
    }
    }
